﻿using Filters;
using Furion.EventBus;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Services;

/// <summary>
/// 商户端 拼团表
/// </summary>
[Authorize, Store, ApiDescriptionSettings("商户")]
public class StoreTeamBuy : BaseServices
{
    private readonly ISqlSugarClient db;
    private readonly IEventPublisher pub;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="db"></param>
    public StoreTeamBuy(ISqlSugarClient db, IEventPublisher pub)
    {
        this.db = db;
        this.pub = pub;
    }

    /// <summary>
    /// 获取所有拼团列表
    /// </summary>
    /// <returns></returns>
    public async Task<object> GetList(int state = -1)
    {
        var data = await db.Queryable<Entity.TeamBuy>()

            .Where(x => x.StoreId == STOREID)

            .WhereIF(state >= 0, x => x.State == (Entity.TeamBuyState)state)

            .Includes(x => x.Product)

            .OrderByDescending(x => x.Id)

            .ToPagedListAsync(PageNo, PageSize);

        return Ok(data);
    }

    /// <summary>
    /// 拼团详情
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<object> Get(int id)
    {
        var data = await db.Queryable<Entity.TeamBuy>()

            .Where(x => x.Id == id && x.StoreId == STOREID)

            .Includes(x => x.Product)

            .Includes(x => x.Store)

            .FirstAsync();

        return Ok(data);
    }

    /// <summary>
    /// 拼团用户列表
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<object> GetTeambuyUsers(int id)
    {
        var data = await db.Queryable<Entity.TeamBuyLog>()

            .Where(x => x.TeamBuyId == id && x.StoreId == STOREID)

            .ToListAsync();

        return Ok(data);
    }



}
